home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / object oriented files / GNUEditDoc.cp < prev    next >
Encoding:
Text File  |  1993-11-10  |  9.5 KB  |  400 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     GNUEditDoc.c
  3.     
  4.     Document methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include <Global.h>
  11. #include <Commands.h>
  12. #include <CApplication.h>
  13. #include <CBartender.h>
  14. #include <CDataFile.h>
  15. #include <CDecorator.h>
  16. #include <CDesktop.h>
  17. #include <CError.h>
  18. #include <CPanorama.h>
  19. #include <CScrollPane.h>
  20. #include <TBUtilities.h>
  21. #include <CEditText.h>
  22. #include <CBureaucrat.h>
  23. #include <CWindow.h>
  24. #include <CEditPane.h>
  25. #include "GNUEditDoc.h"
  26. #include "GNUEditApp.h"
  27. #include "GNUStaticPane.h"
  28.  
  29. #define    WINDculture        500        /* Resource ID for WIND template */
  30.  
  31. extern    CApplication *gApplication;    /* The application */
  32. extern    CBartender    *gBartender;    /* The menu handling object */
  33. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  34. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  35. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  36. extern    OSType        gSignature;        /* The application's signature */
  37. extern    CError        *gError;        /* The error handling object */
  38. extern  CursHandle    gWatchCursor;        /* Watch cursor for waiting            */
  39.  
  40. /***
  41.  * IGNUEditDoc
  42.  *
  43.  *    This is your document's initialization method.
  44.  *    If your document has its own instance variables, initialize
  45.  *    them here.
  46.  *    The least you need to do is invoke the default method.
  47.  *
  48.  ***/
  49.  
  50. void GNUEditDoc::IGNUEditDoc(CApplication *aSupervisor, Boolean printable)
  51.  
  52. {
  53.     CDocument::IDocument(aSupervisor, printable);
  54.     non_static = true;
  55. }
  56.  
  57.  
  58. /***
  59.  * NewFile
  60.  *
  61.  *    When the user chooses New from the File menu, the CreateDocument()
  62.  *    method in your Application class will send a newly created document
  63.  *    this message. This method needs to create a new window, ready to
  64.  *    work on a new document.
  65.  *
  66.  *    Since this method and the OpenFile() method share the code for creating
  67.  *    the window, you should use an auxiliary window-building method.
  68.  *
  69.  ***/
  70. void GNUEditDoc::NewFile(void)
  71.     {
  72.     NewFilefromHandle(NULL);
  73.     }
  74.  
  75. void GNUEditDoc::NewFilefromHandle(Handle theData)
  76. {
  77.     CScrollPane        *theScrollPane;
  78.     Rect            margin;
  79.  
  80.     non_static = true;
  81.     
  82.     if (theData)
  83.         {
  84.         Size theSize = GetHandleSize(theData);
  85.         Ptr thePtr = *theData;
  86.         if (theSize >= kMaxTELength)
  87.                 non_static = false;
  88.  
  89.         while (theSize--)
  90.             {
  91.             if (*thePtr == '\n') *thePtr = '\r';
  92.             thePtr++;
  93.             }
  94.         }    
  95.  
  96.     itsWindow = new(CWindow);
  97.     itsWindow->IWindow(WINDculture, FALSE, gDesktop, this);
  98.  
  99.     theScrollPane = new(CScrollPane);
  100.     
  101.     theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
  102.                                 sizELASTIC, sizELASTIC,
  103.                                 TRUE, TRUE, TRUE);
  104.  
  105.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  106.  
  107.     if (non_static)
  108.         {
  109.         TEHandle macTE;
  110.         CEditPane    *theMainPane = new(CEditPane);
  111.         theMainPane->IEditPane(theScrollPane, this);
  112.         theMainPane->SetFontNumber(monaco);
  113.         theMainPane->SetFontSize(9);
  114.         theMainPane->SetWholeLines(true);
  115.         macTE = ((CEditText *)theMainPane)->macTE;
  116.         (**macTE).crOnly = -1;
  117.         theMainPane->Scroll(-2,-2,false);
  118.         itsMainPane = theMainPane;
  119.         itsGopher = theMainPane;            
  120.         }
  121.     else
  122.         {
  123.         GNUStaticPane    *theMainPane = new(GNUStaticPane);
  124.         theMainPane->IGNUStaticPane(theScrollPane, this);
  125.         itsMainPane = theMainPane;
  126.         itsGopher = theMainPane;
  127.         }
  128.  
  129.     theScrollPane->InstallPanorama((CPanorama *)itsMainPane);
  130.  
  131.     if (theData)
  132.         ((CAbstractText *)itsMainPane)->SetTextHandle(theData);
  133.     
  134.     gDecorator->PlaceNewWindow(itsWindow);
  135.  
  136.     DisposHandle(theData);
  137.  
  138.     itsWindow->Select();
  139. }
  140.  
  141. /***
  142.  * OpenFile
  143.  *
  144.  *    When the user chooses Open… from the File menu, the OpenDocument()
  145.  *    method in your Application class will let the user choose a file
  146.  *    and then send a newly created document this message. The information
  147.  *    about the file is in the SFReply record.
  148.  *
  149.  *    In this method, you need to open the file and display its contents
  150.  *    in a window. This method uses the auxiliary window-building method.
  151.  *
  152.  ***/
  153.  
  154. void GNUEditDoc::OpenFile(SFReply *macSFReply)
  155.  
  156. {
  157.     CDataFile    *theFile;
  158.     Handle        theData;
  159.     Str63        theName;
  160.     OSErr        theError;
  161.  
  162.         /**
  163.          ** Create a file and send it a SFSpecify()
  164.          **    message to set up the name, volume, and
  165.          **    directory.
  166.          **
  167.          **/
  168.  
  169.     theFile = new(CDataFile);
  170.  
  171.         /**
  172.          **    Be sure to set the instance variable
  173.          **    so other methods can use the file if they
  174.          **    need to. This is especially important if
  175.          **    you leave the file open in this method.
  176.          **    If you close the file after reading it, you
  177.          **    should be sure to set itsFile to NULL.
  178.          **
  179.          **/
  180.  
  181.     itsFile = theFile;
  182.  
  183.     theFile->IDataFile();
  184.     theFile->SFSpecify(macSFReply);
  185.     
  186.  
  187.         /**
  188.          **    Send the file an Open() message to
  189.          **    open it. You can use the ReadSome() or
  190.          **    ReadAll() methods to get the contents of the file.
  191.          **
  192.          **/
  193.  
  194.     theFile->Open(fsRdWrPerm);
  195.     
  196.     theData = theFile->ReadAll();     /* ReadAll() creates the handle */
  197.  
  198.     NewFilefromHandle(theData);
  199.  
  200.         /**
  201.          **    In this implementation, we leave the file
  202.          **    open. You might want to close it after
  203.          **    you've read in all the data.
  204.          **
  205.          **/
  206.  
  207.     itsFile->GetName(theName);
  208.     itsWindow->SetTitle(theName);
  209.     
  210.     if (!non_static) 
  211.         {
  212.         itsFile->Close();
  213.         itsFile->Dispose();
  214.         itsFile = NULL;
  215.         }
  216.  
  217. }
  218.  
  219.  
  220.  
  221. /***
  222.  * DoSave
  223.  *
  224.  *    This method handles what happens when the user chooses Save from the
  225.  *    File menu. This method should return TRUE if the file save was successful.
  226.  *    If there is no file associated with the document, you should send a
  227.  *    DoSaveFileAs() message.
  228.  *
  229.  ***/
  230.  
  231. Boolean GNUEditDoc::DoSave(void)
  232.  
  233. {
  234.     Handle        theData;
  235.  
  236.     if (itsFile == NULL)
  237.         return(DoSaveFileAs());
  238.     else {
  239.         theData = ((CAbstractText *)itsMainPane)->GetTextHandle();
  240.         ((CDataFile *)itsFile)->WriteAll(theData);            
  241.         dirty = FALSE;                    /* Document is no longer dirty        */
  242.         gBartender->DisableCmd(cmdSave);
  243.         return(TRUE);                    /* Save was successful                */
  244.     }
  245. }
  246.  
  247.  
  248. /***
  249.  * DoSaveAs
  250.  *
  251.  *    This method handles what happens when the user chooses Save As… from
  252.  *    File menu. The default DoCommand() method for documents sends a DoSaveFileAs()
  253.  *    message which displays a standard put file dialog and sends this message.
  254.  *    The SFReply record contains all the information about the file you're about
  255.  *    to create.
  256.  *
  257.  ***/
  258.  
  259. Boolean GNUEditDoc::DoSaveAs(SFReply *macSFReply)
  260.  
  261. {
  262.         /**
  263.          **    If there's a file associated with this document
  264.          **    already, close it. The Dispose() method for files
  265.          **    sends a Close() message to the file before releasing
  266.          **    its memory.
  267.          **
  268.          **/
  269.          
  270.     if (itsFile != NULL)
  271.         itsFile->Dispose();
  272.  
  273.  
  274.         /**
  275.          **    Create a new file, and then save it normally.
  276.          **
  277.          **/
  278.  
  279.     itsFile = new(CDataFile);
  280.     ((CDataFile *)itsFile)->IDataFile();
  281.     itsFile->SFSpecify(macSFReply);
  282.     if (itsFile->ExistsOnDisk()) itsFile->ThrowOut();
  283.     itsFile->CreateNew(gSignature, 'TEXT');
  284.     itsFile->Open(fsRdWrPerm);
  285.     
  286.     itsWindow->SetTitle(macSFReply->fName);
  287.  
  288.     return( DoSave() );
  289. }
  290.  
  291. /******************************************************************************
  292.  DoCommand {OVERRIDE}
  293.  
  294.         Execute a command
  295.  ******************************************************************************/
  296.  
  297. void GNUEditDoc::DoCommand(long theCommand)
  298.     {
  299.     switch (theCommand) {
  300.         case cmdCPP: case cmdCC1: case cmdCC1MPW: case cmdAS:
  301.             {
  302.             int argc = 0;
  303.             char *argv[500];
  304.             unsigned char *ptr;
  305.             CWindow *newwin;
  306.             Str255    theInput,theOutput;
  307.             void munge(int, char **, int (*)(int,char **), Handle, short optstr);
  308.             Handle stdin = ((CAbstractText *)itsMainPane)->GetTextHandle();
  309.             itsWindow->GetTitle(theInput);
  310.             theInput[1+*theInput] = 0;
  311.             BlockMove(theInput, theOutput, 2+*theInput);
  312.             ptr = &theOutput[*theOutput];
  313.             while ((ptr > theOutput) && (*ptr != '.')) --ptr;
  314.             if (ptr == theOutput) ptr = &theOutput[1+*theOutput];
  315.             switch(theCommand)
  316.                 {
  317.                 case cmdCPP: // "-undef"
  318.                     {
  319.                     int cpp_main(int, char **);
  320.                     BlockMove(".i", ptr, 3);
  321.                     argv[argc++] = "cpp";
  322.                     argv[argc++] = (char *)&theInput[1];
  323.                     argv[argc++] = (char *)&theOutput[1];
  324.                     munge(argc,argv,cpp_main,stdin,theCommand);
  325.                     break;
  326.                     }
  327.                 case cmdCC1: // "-quiet","-mgas","-mintlib","-mnodispatch"
  328.                     {
  329.                     int cc1_main(int, char **);
  330.                     argv[argc++] = "cc1";
  331.                     argv[argc++] = "-mgas";
  332.                     argv[argc++] = (char *)&theInput[1];
  333.                     munge(argc,argv,cc1_main,stdin,theCommand);
  334.                     break;
  335.                     }
  336.                 case cmdCC1MPW: // "-quiet","-mintlib","-mnodispatch"
  337.                     {
  338.                     int cc1_main(int, char **);
  339.                     argv[argc++] = "cc1";
  340.                     argv[argc++] = (char *)&theInput[1];
  341.                     munge(argc,argv,cc1_main,stdin,theCommand);
  342.                     break;
  343.                     }
  344.                 case cmdAS: // "-m68000"
  345.                     {
  346.                     int as_main(int, char **);
  347.                     argv[argc++] = "gas";
  348.                     argv[argc++] = "-o";
  349.                     BlockMove(".o", ptr, 3);
  350.                     argv[argc++] = (char *)&theOutput[1];
  351.                     argv[argc++] = (char *)&theInput[1];
  352.                     munge(argc,argv,as_main,stdin,theCommand);
  353.                     break;
  354.                     }
  355.                 }
  356.             }
  357.             
  358.             break;
  359. #if 0
  360.         case cmdlaunch:
  361.             {
  362.             SFReply     macSFReply;
  363.             Str63        theName;
  364.             OSErr        theError;
  365.             CFile        *theFile;
  366.             Point        corner;                    /* Top left corner of dialog box    */
  367.             Boolean        wasLocked;
  368.             OSType         sfFileTypes[] = {'APPL'};
  369.                                                 /* Center dialog box on the screen    */
  370.             FindDlogPosition('DLOG', getDlgID, &corner);
  371.             
  372.             wasLocked = Lock( TRUE);
  373.             
  374.             SFPGetFile(corner, "\p", NULL, 1, sfFileTypes,
  375.                         NULL, &macSFReply, getDlgID, NULL);
  376.                         
  377.             Lock( wasLocked);
  378.             if (macSFReply.good) 
  379.                     {
  380.                     SetCursor(*gWatchCursor);
  381.                     theFile = new(CFile);
  382.                     theFile->IFile();
  383.                     theFile->SFSpecify(&macSFReply);
  384.                     }
  385.             }
  386.             break;            
  387. #endif
  388.         default:
  389.             inherited::DoCommand(theCommand);
  390.             break;
  391.     }
  392. }
  393.  
  394. void    GNUEditDoc::Notify(CTask        *theTask)
  395. {
  396.     NotifyClean(theTask);
  397.     dirty = non_static;
  398. }
  399.     
  400.